//
//  PYTextFileController.m
//  IHome
//
//   Created by zhujizheng on 2020/2/18.
//  Copyright © 2019年 com.zhujizheng. All rights reserved.
//

#import "PYTextFileController.h"

@interface PYTextFileController ()

@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, copy) NSString *text;
@end

@implementation PYTextFileController

- (BOOL)autoFitSizeForNaviTitle
{
    return YES;
}

+ (instancetype)controllerWithText:(NSString *)text
{
    PYTextFileController *controller = [PYTextFileController new];
    controller.text = text;
    return controller;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTextView];
    self.textView.text = self.text;
    [self addRightBarButtonItemWithImage:[UIImage imageNamed:@"more"] action:@selector(moreAction)];
}

- (void)moreAction
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"更多操作" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *scrollToBottomAction = [UIAlertAction actionWithTitle:@"滚动到底部" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self.textView scrollToBottomAnimated:NO];
        // TODO iPad上非要写两句代码才能滚到底部，否则没任何反应
        [self.textView scrollToBottomAnimated:NO];
    }];
    [alertController addAction:scrollToBottomAction];
    UIAlertAction *scrollToTopAction = [UIAlertAction actionWithTitle:@"滚动到顶部" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self.textView scrollToTopAnimated:NO];
    }];
    [alertController addAction:scrollToTopAction];
    UIAlertAction *uploadAction = [UIAlertAction actionWithTitle:@"上传日志" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self uploadLog];
    }];
    [alertController addAction:uploadAction];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    alertController.popoverPresentationController.sourceView = self.view;
    alertController.popoverPresentationController.sourceRect = CGRectMake(self.view.width / 2, self.view.height / 2, 1, 1);
    alertController.popoverPresentationController.permittedArrowDirections = 0;
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)uploadLog
{
    
}

#pragma mark - textView
- (UITextView *)textView
{
    if (!_textView) {
        _textView = [UITextView new];
        _textView.textColor = PY_Dark_Color;
        _textView.font = PY_Font_Regular(12);
        _textView.editable = NO;
    }
    return _textView;
}

- (void)initTextView
{
    [self.view addSubview:self.textView];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

@end
